home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / Scanner / Scan.0.91 / Source / scan_control.m < prev    next >
Text File  |  1996-02-03  |  5KB  |  213 lines

  1.  
  2. #import "scan_control.h"
  3. #import <bsd/dev/scsireg.h>
  4. #import <sys/param.h>
  5. #import "scanView.h"
  6. #import "ScanDisplay.h"
  7. #import "DisplayControl.h"
  8.  
  9. @implementation scan_control
  10.  
  11. - init
  12. {
  13.     [super init];
  14.     [self findScanner];
  15.     scanImage = 0x0;
  16.     return self;
  17. }
  18.  
  19. - doScan:sender
  20. {
  21.     int xstart = [xstartText intValue];
  22.     int ystart = [ystartText intValue];
  23.     int width = [widthText intValue];
  24.     int length = [lengthText intValue];
  25.  
  26.     scanImage = (NXBitmapImageRep *)[scanner performScan:xstart :ystart
  27.                                                         :width :length
  28.                                                         preview:NO ];
  29.     if (scanImage )
  30.     {
  31.        [scanDisplay drawBitmap:scanImage :xstart :ystart];
  32.        [self newDisplay:self];
  33.     }
  34.     return self;
  35. }
  36.  
  37. - doPreview:sender
  38. {
  39.     scanImage = (NXBitmapImageRep *)[scanner performPreview];
  40.     if (scanImage )
  41.     {
  42.        [scanDisplay drawBitmap:scanImage :0 :0];
  43.        [scanImage free];
  44.     }
  45.     return self;
  46. }
  47.  
  48. - photocopy:sender
  49. {
  50.     int xstart = [xstartText intValue];
  51.     int ystart = [ystartText intValue];
  52.     int width = [widthText intValue];
  53.     int length = [lengthText intValue];
  54.  
  55.     scanImage = (NXBitmapImageRep *)[scanner performScan:xstart :ystart
  56.                                                         :width :length
  57.                                                         preview:NO ];
  58.     if (scanImage )
  59.     {
  60.        [scanDisplay drawBitmap:scanImage :xstart :ystart];
  61.        [[NXApp printInfo] setMarginLeft:0 right:0 top:0 bottom:0];
  62.        [scanDisplay printPSCode:nil];
  63.        [scanImage free];
  64.     }
  65.  
  66.     return self;
  67. }
  68.  
  69. - findScanner
  70. {
  71.     struct scsi_adr sa;
  72.     struct scsi_req sr;
  73.     struct cdb_6 *cdbp = &sr.sr_cdb.cdb_c6;
  74.  
  75.     char returnData[148];           // Read the inquiry response into here.
  76.     char* name;
  77.     int target;
  78.     int scannerFD;
  79.     
  80.     // Can we open the generic scsi device? If not, we pop up a
  81.     // panel and then quit.
  82.     
  83.     if ((scannerFD = open("/dev/sg0", O_RDWR, 0666)) <= 0)
  84.     {
  85.         NXRunAlertPanel("Error",
  86.                         "Unable to open scanner device /dev/sg0",
  87.                         "OK", NULL, NULL);
  88.         exit(-1);
  89.     }
  90.     
  91.     // Start scanning through all the scsi devices. Assume (of course)
  92.     // that the scanner will be at LUN 0.
  93.     
  94.     for(target = 0; target <= 7; target++)
  95.     {
  96.         sa.sa_target = target;
  97.         sa.sa_lun = 0;
  98.  
  99.         // if a device is present at the target number....
  100.         // (this ioctl binds the sg controller to the target #.
  101.         
  102.         if (ioctl(scannerFD, SGIOCSTL, &sa) >= 0)
  103.         {
  104.             bzero(&sr, sizeof(struct scsi_req));
  105.  
  106.             cdbp->c6_opcode = C6OP_INQUIRY;         // Sending inquiry command
  107.             cdbp->c6_lun = 0;                       // Assume LUN = 0
  108.             cdbp->c6_len = sizeof(returnData);     // Number of bytes from device
  109.  
  110.             sr.sr_dma_dir = SR_DMA_RD;              // It's a read function
  111.             sr.sr_addr    = (char *) &returnData;    // with data ending up here.
  112.             sr.sr_dma_max = cdbp->c6_len;           // and it's this long.
  113.             sr.sr_ioto    = 2;                        // Should respond within 2 seconds.
  114.  
  115.             // If the device responded adequately...
  116.             
  117.             if (ioctl(scannerFD, SGIOCREQ, &sr) >= 0)
  118.             {
  119.                 // returning good status, check to see if it's our scanner.
  120.                 if(!sr.sr_io_status)
  121.                 {
  122.                     if ( (returnData[0x00] & 0x1F) == DEVTYPE_SCANNER)
  123.                     {
  124.                       close(scannerFD);
  125.                       name = &returnData[8];
  126.                       // [self loadScannerBundle:&returnData[8] ];
  127.                       // [self loadScannerBundle:name ];
  128.                       name = calloc(10, sizeof(char));
  129.                       sprintf(name, "UMAX");
  130.                       [self loadScannerBundle:name];
  131.                       return self;
  132.                     }
  133.                 }
  134.             }
  135.         }
  136.     }
  137.  
  138.     NXRunAlertPanel("Error","No supported scanner found!", "OK", NULL, NULL);
  139.  
  140.     [NXApp terminate:self];
  141.     return self;
  142. }
  143.  
  144. - loadScannerBundle:(char *)name
  145. {
  146.     char currentPath[MAXPATHLEN+1]; // path to current (app) directory
  147.     char message[80];
  148.     char *suffix;
  149.     NXBundle *myBundle = 0;
  150.  
  151.     // suffix = index(name, ' ');                // end of scanner name
  152.     suffix = name + 8;
  153.     *suffix = '\0';                           // terminate after name
  154.     strcat(name, ".bundle\0");               // append .bundle suffix
  155.     strcpy(currentPath, NXArgv[0]);            // get path to app
  156.     suffix = rindex(currentPath, '/') + 1;    // find last slash (plus 1 character)
  157.     *suffix = '\0';                            // eliminate the app name
  158.     strcat(currentPath, name);               // append .bundle suffix
  159.     
  160.     myBundle = [[NXBundle alloc] initForDirectory:currentPath];   // get a bundle
  161.     scanner = [[[ myBundle principalClass] alloc] init];          // and load the class
  162.  
  163.     if (scanner == 0)
  164.     {
  165.         sprintf(message, "Couldn't locate %s!", name);
  166.         NXRunAlertPanel("Error", message, "OK", NULL, NULL);
  167.         [NXApp terminate:self];
  168.     }
  169.  
  170.     if (![[scanner class] conformsTo:@protocol(Scanner)])
  171.     {
  172.         sprintf(message, "Found scanner, but %s\ndoes not conform to Scanner protocol!", name);
  173.         NXRunAlertPanel("Error", message, "OK", NULL, NULL);
  174.         [NXApp terminate:self];
  175.     }
  176.         
  177.     [myBundle free];
  178.     return self;
  179. }
  180.  
  181. - newDisplay:sender
  182. {
  183.     id win;
  184.     if ([NXApp loadNibSection: "scanDisplay.nib" owner:self] == nil)
  185.     {
  186.         return nil;
  187.     }
  188.     
  189.     if ([newDisplay setUp:scanImage])
  190.     {
  191.         win = [newDisplay window];
  192.         if (win)
  193.         {
  194.             NXRect frame;
  195.             char buf[256];
  196.             
  197.             [win getFrame: &frame];
  198.             NX_X(&frame) += offset;
  199.             NX_Y(&frame) -= offset;
  200.             if ( (offset += 25.0) > 100.0) offset = 0.0;
  201.             
  202.             sprintf(buf, [win title], ++scanNum);
  203.             [win setTitle: buf];
  204.             [win placeWindowAndDisplay: &frame];
  205.             [win makeKeyAndOrderFront: nil];
  206.             return newDisplay;
  207.         }
  208.     }
  209.     return nil;
  210. }
  211.  
  212. @end
  213.